<?php
namespace App\Http\Controllers\NewAdmin;

use App\Http\Controllers\AdapterWebController;
use Illuminate\Http\Request;
use App\Modules\Adv\Adv as ApiAdv;

class Adv extends AdapterWebController
{
    // 广告列表
    public function advList()
    {
        $list = $this->appLayerExec(ApiAdv::class, 'advList');
        return view('newAdmin.adv.adv_list', ['list' => $list['res']]);
    }

    // 新增广告
    public function addAdv(Request $request)
    {
        if ($request->isMethod('get')) {
            return view('newAdmin.adv.adv_add');
        }

        $postData = $request->all();
        $res = $this->appLayerExec(ApiAdv::class, 'addAdv', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '广告增加成功!'];
        }
        return ['status' => -1, 'msg' => '广告增加失败，请重试!'];
    }

    // 文件(图片)上传
    public function upload(Request $request)
    {
        $fileRes = $this->fileUpload($request, 'files');
        if (!$fileRes['status']) {
            return ["code" => 1, "msg" => "图片上传失败！"];
        }

        return ["code" => 0, "msg" => "图片上传成功！", "url" => $fileRes['url']];
    }

    // 禁用
    public function disable($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiAdv::class, 'disableAdv', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '广告已禁用!'];
        }
        return ['status' => -1, 'msg' => '广告禁用异常，请重试!'];
    }

    // 启用
    public function enable($id)
    {
        $postData['id'] = (int)$id;
        $res = $this->appLayerExec(ApiAdv::class, 'enableAdv', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '广告已启用!'];
        }
        return ['status' => -1, 'msg' => '广告启用异常，请重试!'];
    }

    // 编辑广告
    public function editAdv(Request $request, $id)
    {
        if ($request->isMethod('get')) {
            $postData['id'] = (int)$id;
            $res = $this->appLayerExec(ApiAdv::class, 'advInfo', $postData);
            if ($res['code'] == 200) {
                return view('newAdmin.adv.adv_edit', ['info' => $res['res']]);
            }
            return false;
        }

        $postData = $request->all();
        $postData['id'] = $id;
        $res = $this->appLayerExec(ApiAdv::class, 'editAdv', $postData);
        if ($res['code'] == 200) {
            return ['status' => 0, 'msg' => '文章保存成功!'];
        }
        return ['status' => -1, 'msg' => '文章信息修改异常，请重试!'];
    }
}
